/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package CL;

import EL.TableTypes;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import slsBeans.TableTypeBLORemote;

/**
 *
 * @author Tian
 */
public class TableTypeCO extends HttpServlet {
    private TableTypeBLORemote rTableType;

    @Override
    public void init() throws ServletException
    {
        try
        {
            super.init();
            String jndiFileName = this.getServletContext().getRealPath("/") + "/WEB-INF/jndi.properties";
            Properties props = new Properties();
            props.load(new FileInputStream(jndiFileName));
            InitialContext ctx = new InitialContext(props);
            rTableType = (TableTypeBLORemote) ctx.lookup("TableTypeBLO/remote");
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String action = request.getParameter("action");
        if (action.equalsIgnoreCase("getAll"))
        {
            List<TableTypes> tableTypes = rTableType.getAll();
            request.setAttribute("tableTypes", tableTypes);
            RequestDispatcher rd = request.getRequestDispatcher("ad_TableType_Viewing.jsp");
            rd.forward(request, response);
        }
        else if(action.equalsIgnoreCase("formAddNew"))
        {
            response.sendRedirect("ad_TableType_Adding.jsp");
        }
        else if(action.equalsIgnoreCase("add"))
        {
            String tableType = request.getParameter("txtTableType");
            String description = request.getParameter("txtDescription");
            TableTypes type = new TableTypes(tableType, description);
            boolean result = rTableType.add(type);
            if(result)
            {
                response.sendRedirect("TableTypeCO?action=getAll");
                //msg insert successfull
            }
            else
            {
                response.sendRedirect("TableTypeCO?action=formAddNew");
                //msg insert fail
            }
        }
        else if (action.equalsIgnoreCase("formEditTableType"))
        {
            String id = request.getParameter("id");
            TableTypes tableType = rTableType.getByID(Integer.parseInt(id));
            request.setAttribute("tableType", tableType);
            RequestDispatcher rd = request.getRequestDispatcher("ad_TableType_Editing.jsp");
            rd.forward(request, response);
        }
        else if(action.equalsIgnoreCase("edit"))
        {
            String id = request.getParameter("txtID");
            String type = request.getParameter("txtTableType");
            String description = request.getParameter("txtDescription");
            TableTypes tableType = rTableType.getByID(Integer.parseInt(id));
            if(tableType != null)
            {
                tableType.setType(type);
                tableType.setDescription(description);
                boolean result = rTableType.edit(tableType);
                if(result)
                {
                    response.sendRedirect("TableTypeCO?action=getAll");
                }
                else
                {
                    response.sendRedirect("TableTypeCO?action=formEditTableType");
                }
            }
        }
        else if(action.equalsIgnoreCase("delete"))
        {
            String id = request.getParameter("id");
            boolean result = rTableType.delete(Integer.parseInt(id));
            if(result)
            {
                response.sendRedirect("TableTypeCO?action=getAll");
            }
            else
            {
                response.sendRedirect("TableTypeCO?action=getAll");
            }
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
